-- M$SQL
-- DROP ANSWER TABLE

USE [cfExam]

IF  EXISTS (
    SELECT * 
    FROM sys.foreign_keys 
    WHERE object_id = OBJECT_ID(N'[dbo].[FK_answer_question]') 
    AND parent_object_id = OBJECT_ID(N'[dbo].[answer]')
)
ALTER TABLE [dbo].[answer] DROP CONSTRAINT [FK_answer_question]
GO
IF  EXISTS (
    SELECT * FROM sys.objects 
    WHERE object_id = OBJECT_ID(N'[dbo].[answer]') AND type in (N'U')
)
DROP TABLE [dbo].[answer]
GO

-- DROP ATTEMPT_QUESTION TABLE
IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_attempt_question_attempt]') AND parent_object_id = OBJECT_ID(N'[dbo].[attempt_question]'))
ALTER TABLE [dbo].[attempt_question] DROP CONSTRAINT [FK_attempt_question_attempt]
GO
IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_attempt_question_question]') AND parent_object_id = OBJECT_ID(N'[dbo].[attempt_question]'))
ALTER TABLE [dbo].[attempt_question] DROP CONSTRAINT [FK_attempt_question_question]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[attempt_question]') AND type in (N'U'))
DROP TABLE [dbo].[attempt_question]
GO

-- DROP EXAM TABLE
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[exam]') AND type in (N'U'))
DROP TABLE [dbo].[exam]
GO

-- DROP QUESTION TABLE
IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_question_exam]') AND parent_object_id = OBJECT_ID(N'[dbo].[question]'))
ALTER TABLE [dbo].[question] DROP CONSTRAINT [FK_question_exam]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[question]') AND type in (N'U'))
DROP TABLE [dbo].[question]
GO

-- DROP USER_EXAM TABLE
IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_user_exam_exam]') AND parent_object_id = OBJECT_ID(N'[dbo].[user_exam]'))
ALTER TABLE [dbo].[user_exam] DROP CONSTRAINT [FK_user_exam_exam]
GO
IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_user_exam_users]') AND parent_object_id = OBJECT_ID(N'[dbo].[user_exam]'))
ALTER TABLE [dbo].[user_exam] DROP CONSTRAINT [FK_user_exam_users]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[user_exam]') AND type in (N'U'))
DROP TABLE [dbo].[user_exam]
GO

-- DROP USERS TABLE
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[users]') AND type in (N'U'))
DROP TABLE [dbo].[users]
GO

-- DROP ATTEMPT TABLE
IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_attempt_users]') AND parent_object_id = OBJECT_ID(N'[dbo].[attempt]'))
ALTER TABLE [dbo].[attempt] DROP CONSTRAINT [FK_attempt_users]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[attempt]') AND type in (N'U'))
DROP TABLE [dbo].[attempt]
GO
